package com.huiqiao.lesson9.service;

import com.huiqiao.lesson9.aop.APIException;
import com.huiqiao.lesson9.entity.SysUser;
import com.huiqiao.lesson9.mapper.SysUserMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Log4j2
@Service
public class SysUserService {

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 注册
     * @param user
     * @return
     */
    public SysUser register(SysUser user) {
        SysUser oldUser = userMapper.selectUserByUserName(user.getUserName());
        if (oldUser != null) {
            throw new RuntimeException("注册失败，username已存在");
        }

        oldUser = userMapper.selectUserByPhone(user.getPhonenumber());
        if (oldUser != null) {
            throw new RuntimeException("注册失败，phonenumber已存在");
        }

        user.setUserType("01");
        int row = userMapper.insertUser(user);
        if (row < 1) {
            throw new RuntimeException("注册失败，数据库插入失败");
        }
        return user;
    }


    /**
     * 注册
     * @param user
     * @return
     */
    public SysUser register2(SysUser user) {
        throw new APIException(310, "测试一下");
    }




}
